package com.qs.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.BaseDAO;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.qs.po.QsRecSignin;
import com.qs.vo.QsRecSigninItem;

public class QsRecSigninDAO extends BaseDAO<QsRecSignin> {

	@SuppressWarnings("unchecked")
	public QsRecSigninItem find(int id) throws DaoException {
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("id");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] {Department.class },
					QsRecSignin.class, new String[] {"deptId" },
					new String[] {"deptId" }, searchBean);
			List<QsRecSigninItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<QsRecSigninItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] {Department.class },
					QsRecSignin.class, new String[] {"deptId" },
					new String[] {"deptId" });
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<QsRecSigninItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] {Department.class },
				QsRecSignin.class, new String[] {"deptId" },
				new String[] {"deptId" }, searchBean);
		return parseResult(result);
	}
	
	@SuppressWarnings("unchecked")
	private List<QsRecSigninItem> parseResult(List result){
		Iterator it = result.iterator();
		List<QsRecSigninItem> list = new ArrayList<QsRecSigninItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			QsRecSignin bean = (QsRecSignin) o[0];
			Department dept = (Department) o[1];
			QsRecSigninItem item = new QsRecSigninItem();
			item.setBean(bean);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] {Department.class },
				QsRecSignin.class, new String[] {"deptId" },
				new String[] {"deptId" }, searchBean);
	}
}
